package user;

import Dao.communityDao;
import Dao.communityDaoImpl;
import been.Announcements;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet(value = "/editAnnouncement")
public class editAnnouncementServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        communityDao dao = new communityDaoImpl();
        int announcement_id = Integer.parseInt(request.getParameter("announcement_id"));

        try {
            request.setAttribute("announcement",dao.findAnnouncementByAnnouncementId(announcement_id));
        } catch (SQLException e) {
                throw new RuntimeException(e);
        }
        request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        communityDao dao = new communityDaoImpl();
        Announcements announcement = null;
        String message = null;
        int announcement_id=0;
        String announcement_idParam = request.getParameter("announcement_id");
        if (announcement_idParam ==null|| announcement_idParam.isEmpty()) {
            message="id不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request, response);
            return;
        }
        else {
            try{
                announcement_id=Integer.parseInt(announcement_idParam);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        String title=request.getParameter("title");
        if (title==null||title.isEmpty()) {
            message="标题不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request, response);
            return;
        }
        String content=request.getParameter("content");
        if (content==null||content.isEmpty()) {
            message="内容不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request, response);
            return;
        }
        announcement=new Announcements(announcement_id,title,content);
        try {
            int res=dao.updateAnnouncement(announcement);
            if(res>0){
                message="修改成功";
                request.setAttribute("message",message);
                request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request,response);
            }
            else
            {
                message="修改失败";
                request.setAttribute("message",message);
                request.getRequestDispatcher("/WEB-INF/user/editAnnouncement.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
